// fenxiao/jjkData/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staffCount: 0,
    userCount: 0,
    regionId: '',
    layer: 3,
    city: ''
  },

  changeCity: function(){
    wx.redirectTo({
      url: '../myAllCity/index?regionId=' + this.data.regionId,
    })
  },

  getTotalData: function (regionid=0, layer=3) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/wxstore/CardReport?parentUid=0&regionId=' + regionid+'&layer=' + layer,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            that.setData({
              staffCount: res.data.Data.StaffCount,
              userCount: res.data.Data.UserCount,
              localStaffCount: res.data.Data.LocalStaffCount,
              localUserCount: res.data.Data.LocalUserCount
            })
          }
        }
      },
    })
  },

  getCityBoss: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/distributor/GetDisRegionList?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data && res.data.Data.length > 0) {
            let PCList = res.data.Data;
            that.data.regionId = PCList[0].RegionId;
            that.getTotalData(PCList[0].RegionId,PCList[0].Layer);
            that.setData({
              city: PCList[0].RegionName,
              layer: PCList[0].Layer
            })
          }else{
            that.getTotalData();
          }
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.regionId){
      //this.data.regionId = options.regionId;
      let layar = options.city.indexOf('市') > -1 ? 2 : this.data.layer;
      this.setData({
        city: options.city,
        regionId: options.regionId,
        layer: layar
      })
      this.getTotalData(options.regionId, layar);
    }else{
      this.getCityBoss();
    }
    
  },

  toMerList: function () {
    wx.navigateTo({
      url: '../settledBusiness/index?regionId=' + this.data.regionId + '&layer=' + this.data.layer + '&isBoss=1',
    })
  },

  toMember: function () {
    wx.navigateTo({
      url: '../jjCardMember/index?regionId=' + this.data.regionId + '&layer=' + this.data.layer + '&isBoss=1',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})